#include <math.h>
#include "PressureButtonObject.h"
#include "include.h"

PressureButtonObject::PressureButtonObject(char * name, TextureManager * textureManager, Dictionary * gameState,
					Vector2D position,
					Vector2D * vertices,
					int numVertices,
					float mass,
					bool isGrabbable,
					Vector2D movementAxis,
					Vector2D movementEndpoints,
					bool offRight,
					bool sticky,
					float springConstant,
					char * textureName,
					Vector2D * textureVertices)
: ConstrainedObject(textureManager, position, vertices, numVertices, mass, isGrabbable, false,
 					movementAxis, movementEndpoints, textureName, textureVertices)
 {
 	collideWithGround = false;
 	this->isActive = false;
 	this->offRight = offRight;
 	this->isSticky = sticky;
 	this->springConstant = springConstant;
 	this->material->SetFriction(500.0);
 	gameState->Add(name, (void*)&(this->isActive), BOOL_TYPE_STRING);
 	dampingConstant = 1.5 * sqrt(springConstant * mass);
 	
 	
 	//Can we have a skirt? If so, find the skirt vertices.
 	isSkirted = false;
 	rightSkirtVertex = NULL;
 	leftSkirtVertex = NULL;
 	
 	if(!freeRotate)
 	{
 		for(int i=0; i < numVertices-1; i++)
 		{
 			Vector2D direction = vertices[i+1] - vertices[i];
 			direction = !direction;
 		
 			printf("Is it orthogonal? %f\n", this->movementAxis.dot(direction));
 			float dp = this->movementAxis.dot(direction);
	 		if(dp <= 0.05 && dp >= -0.05)
	 		{
	 			//orthogonal
	 			Vector2D midpoint = (vertices[i] + vertices[i+1]) * 0.5;
	 			
	 			printf("Is it in the right direction? %f\n",this->movementAxis.dot(midpoint));
	 			if(this->movementAxis.dot(midpoint) < 0)
	 			{
	 				//found it
	 				printf("Yes!\n");
	 				isSkirted = true;
	 				
	 				leftSkirtVertex = &vertices[i];
	 				rightSkirtVertex = &vertices[i+1];
	 				leftSkirtTextureVertex = &textureVertices[i];
	 				rightSkirtTextureVertex = &textureVertices[i+1];
	 				leftSkirtTexturePosition = textureVertices[i];
	 				rightSkirtTexturePosition = textureVertices[i+1];
	 				
	 				if(offRight)
	 				{
	 					//skirt around the left
	 					leftSkirtPosition = leftMovementEndpoint + (*leftSkirtVertex);
	 					rightSkirtPosition = leftMovementEndpoint + (*rightSkirtVertex);
	 				}
	 				else
	 				{
	 					//skirt around the right
	 					leftSkirtPosition = rightMovementEndpoint + (*leftSkirtVertex);
	 					rightSkirtPosition = rightMovementEndpoint + (*rightSkirtVertex);
	 				}
	 				break;	
	 			}
	 		}	
 		}
 	}
 }
PressureButtonObject::~PressureButtonObject()
{
}
bool PressureButtonObject::PreDrawEntryPoint()
{
	//change the object's vertices so that it covers its skirt.
	if(isSkirted )
	{
		*leftSkirtVertex = leftSkirtPosition - position;
		*rightSkirtVertex = rightSkirtPosition - position;
		
		float fullDist = rightMovementEndpoint.dist(leftMovementEndpoint);
		float fraction = 1.0;
		if(!offRight)
		{	
			fraction = (rightMovementEndpoint.dist(position)/fullDist);
		}
		else
		{
			fraction = (leftMovementEndpoint.dist(position)/fullDist);
		}
		Vector2D ortho = Vector2D(-movementAxis.y, movementAxis.x);
		*leftSkirtTextureVertex = 	movementAxis * (movementAxis.dot(leftSkirtTexturePosition) * fraction) + ortho * (ortho.dot(leftSkirtTexturePosition));
		*rightSkirtTextureVertex = 	movementAxis * (movementAxis.dot(rightSkirtTexturePosition) * fraction) + ortho * (ortho.dot(rightSkirtTexturePosition));
	}
	return false;	
}
bool PressureButtonObject::PreThinkEntryPoint(double elapsedTime)
{	
	Vector2D springOrigin = rightMovementEndpoint;
	if(!offRight)
	{
		springOrigin = leftMovementEndpoint;
	}
	
	Vector2D direction = springOrigin - position;
	
	
	AddForce( direction * springConstant );
	AddForce( velocity * -dampingConstant);
	
	isActive = (rightMovementEndpoint.dist(position) / rightMovementEndpoint.dist(leftMovementEndpoint)) > 0.5;
	
	return false;
}
